package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.ConfigCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * config
 */
public class Config {

	public static final String TABLENAME = "config";
	public static final String CLASSNAME = "Config"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** k */
	private String k;
	
	/** v */
	private String v;
	
	/** remark */
	private String remark;
	
	
	public Config() {
		super();
	}
	
	public Config(int id, String k, String v, String remark) {
		super();
		this.id = id;
		this.k = k;
		this.v = v;
		this.remark = remark;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public String getK() {
		return k;
	}
	
	public void setK(String k) {
		this.k = k;
		addUpdateColumn("k",k);
	}
	
	public String getV() {
		return v;
	}
	
	public void setV(String v) {
		this.v = v;
		addUpdateColumn("v",v);
	}
	
	public String getRemark() {
		return remark;
	}
	
	public void setRemark(String remark) {
		this.remark = remark;
		addUpdateColumn("remark",remark);
	}
	
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("k", this.k);
        result.put("v", this.v);
        result.put("remark", this.remark);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("k", this.k);
        result.put("v", this.v);
        result.put("remark", this.remark);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeString(out,this.k);
		    SK_OutputStream.writeString(out,this.v);
		    SK_OutputStream.writeString(out,this.remark);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Config createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Config config = new Config();
		    config.id = SK_InputStream.readInt(in,null);
		    config.k = SK_InputStream.readString(in,null);
		    config.v = SK_InputStream.readString(in,null);
		    config.remark = SK_InputStream.readString(in,null);
		    return config;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Config> createForColumnNameList(List<Map<String, Object>> list){
    	List<Config> configs = new ArrayList<Config>();
		for (Map<String, Object> map : list) {
			configs.add(createForColumnNameMap(map));
		}
		return configs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Config createForColumnNameMap(Map<String, Object> map){
    	Config obj = new Config();
	    obj.id = SK_Map.getInt("id", map);
	    obj.k = SK_Map.getString("k", map);
	    obj.v = SK_Map.getString("v", map);
	    obj.remark = SK_Map.getString("remark", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Config> createForList(List<Map<String, Object>> list){
    	List<Config> configs = new ArrayList<Config>();
		for (Map<String, Object> map : list) {
			configs.add(createForColumnNameMap(map));
		}
		return configs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Config createForMap(Map<String, Object> map){
    	Config obj = new Config();
	    obj.id = SK_Map.getInt("id", map);
	    obj.k = SK_Map.getString("k", map);
	    obj.v = SK_Map.getString("v", map);
	    obj.remark = SK_Map.getString("remark", map);
        return obj;
    }
    
    public static Config createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Config> createForJson(List<String> jsons){
    	List<Config> configs = new ArrayList<Config>();
    	for(String json : jsons){
    		configs.add(createForJson(json));
    	}
    	return configs;
    }
    
    /** 延迟插入数据库 */
    public Config insert(){
    	return ConfigCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Config update(){
    	return ConfigCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return ConfigCache.delete(this);
    }
    /** 即时插入数据库 */
    public Config insertAndFlush(){
    	return ConfigCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Config updateAndFlush(){
    	return ConfigCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return ConfigCache.deleteAndFlush(this);
    }
}